package com.piotrs10.bsk.dao.tests;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.piotrs10.bsk.dao.BaseDao;
import com.piotrs10.bsk.model.permissions.ObiektDB;

public class ObiektyDao extends BaseDao {

	private final String ALL_OBJECTS_QUERY = "SELECT * FROM obiekt WHERE id = ?";

	public List<ObiektDB> getAllObjects() {
		List<ObiektDB> objectsList = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = this.getConnection();
			statement = connection.prepareStatement(this.ALL_OBJECTS_QUERY);
			statement.setInt(1, 1);
			ResultSet set = statement.executeQuery();
			objectsList = new ArrayList<ObiektDB>(set.getFetchSize());
			while (set.next()) {
				ObiektDB obiektDB = new ObiektDB(set.getInt(1),
						set.getString(2));
				objectsList.add(obiektDB);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					System.err.println(e.getMessage());
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					System.err.println(e.getMessage());
				}
			}
		}
		return objectsList;
	}
}
